package com.macro.mall.controller;

import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.model.UmsServiceDept;
import com.macro.mall.service.UmsServiceDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.security.Principal;
import java.util.List;

/**
 * UmsServiceDept的路由接口服务
 *
 * @author
 */
@RestController
@RequestMapping("serviceDept")
@Api(tags = "UmsServiceDeptController", description = "服务机构管理")
public class UmsServiceDeptController {

    /**
     * UmsServiceDeptService服务
     */
    @Autowired
    private UmsServiceDeptService umsServiceDeptService;

    /**
     * 查询所有UmsServiceDept数据的方法
     *
     * @param
     * @return
     */
    @ResponseBody
    @ApiOperation("查询服务机构列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult find(@ApiParam(value = "服务机构名称") @RequestParam(value = "deptName", defaultValue = "") String deptName,
                             @ApiParam(value = "01-未合作 02-合作中 03-审核中") @RequestParam(value = "status", defaultValue = "") String status,
                             @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                             @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        UmsServiceDept build = UmsServiceDept.builder()
                .deptName(deptName)
                .status(status)
                .build();
        List<UmsServiceDept> umsServiceDepts = umsServiceDeptService.find(build, pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(umsServiceDepts));
    }

    /**
     * 通过id查询UmsServiceDept数据的方法
     *
     * @param id
     * @return
     */

    @ApiOperation("通过id查询服务机构")
    @RequestMapping(value = "/findOneByid", method = RequestMethod.GET)
    public CommonResult findOne(@RequestParam(value = "id") Integer id) {
        UmsServiceDept one = umsServiceDeptService.findOne(id);
        return CommonResult.success(one);
    }

    /**
     * 插入UmsServiceDept属性不为空的数据方法
     *
     * @param value
     * @return
     */
    @ApiOperation("新增")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public CommonResult save(Principal principal, @Valid @RequestBody UmsServiceDept value) {
        if(principal==null){
            return CommonResult.unauthorized(null);
        }
        return umsServiceDeptService.saveNotNull(value);
    }

    /**
     * 更新UmsServiceDept属性不为空的数据方法
     *
     * @param value
     * @return
     */
    @ApiOperation("修改")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public CommonResult update(Principal principal, @Valid @RequestBody UmsServiceDept value) {
        if(principal==null){
            return CommonResult.unauthorized(null);
        }
        return umsServiceDeptService.updateNotNullById(value);
    }

    /**
     * 更新UmsServiceDept属性不为空的数据方法
     *
     * @param value
     * @return
     */
    @ApiOperation("修改状态")
    @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
    public CommonResult updateStatus(Principal principal, @RequestBody UmsServiceDept value) {
        if(principal==null){
            return CommonResult.unauthorized(null);
        }
        return umsServiceDeptService.updateStatusById(value);
    }

    /**
     * 通过id删除UmsServiceDept数据方法
     *
     * @param id
     * @return
     */
    @ApiOperation("删除")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public CommonResult delete(@RequestParam(value = "id") Integer id) {
        return umsServiceDeptService.deleteById(id);
    }
}
